library(tidyverse)
library(car)

survey <- read.table("Harris_Data/Harris 1980 ABC News Energy Crisis and Economy Survey, study no. 802114/harris_s802114_spss.tab", header = TRUE)

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- "802114"

# study year (year)
survey$year <- 1980

# geographic data (urban)
survey$urban <- dplyr::recode(survey$S13,
                              `4` = "Rural",
                              `2` = "Suburb",
                              `1` = "Central city",
                              `3` = "Town")

# geographic data (region)
survey$region <- dplyr::recode(survey$S11,
                               `4` = "South",
                               `1` = "East",
                               `3` = "South",
                               `7` = "West",
                               `6` = "Midwest",
                               `2` = "East",
                               `5` = "Midwest",
                               `8` = "West")

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
survey$inequality <- as.character(dplyr::recode(survey$Q4_2, 
                                  `1` = "Feel",
                                  `2` = "Don't feel",
                                  `3` = "Not sure"))
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <-  dplyr::recode(survey$F3_1,
                                   `1` = "Yes",
                                   `0` = "No")
survey$union.self[survey$F3_4 == 1] <- "Not Sure"

survey$union.other <-  dplyr::recode(survey$F3_2,
                                   `1` = "Yes",
                                   `0` = "No")
survey$union.other[survey$F3_4 == 1] <- "Not Sure"

table(survey$union.self)
table(survey$union.other)

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
summary(survey$F1)
table(survey$F1)
survey$occupation <- as.character(dplyr::recode(survey$F1,
                                          `1` = "Professional",
                                          `2` = "Manager, official",
                                          `3` = "Proprietor (small business)",
                                          `4` = "Clerical worker",
                                          `5` = "Sales worker",
                                          `6` = "Skilled craftsman, foreman",
                                          `7` = "Operative, unskilled laborer (except farm)", 
                                          `8` = "Service worker",
                                          `9` = "Farmer, farm manager, farm laborer",
                                          `10` = "Student",
                                          `11` = "Housewife",
                                          `12` = "Military service",
                                          `13` = "Unemployed",
                                          `14` = "Retired",
                                          `15` = "Welfare",
                                          `16` = "Disabled",
                                          `17` = "Other (SPECIFY)",
                                          `18` = "Not sure/refused"))
table(survey$occupation)

# occ self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
table(survey$F2)
survey$educ <- dplyr::recode(survey$F2, 
                           `1` = "No formal schooling",
                           `2` = "1-7 years completed",
                           `3` = "8 years completed",
                           `4` = "some high school",
                           `5` = "high school graduate",
                           `6` = "Some college",
                           `7` = "2-year college graduate",
                           `8` = "4-year college graduate",
                           `9` = "Post graduate",
                           `10` = "Trade/technical/vocational school after high school")
table(survey$educ)
                        

# household income (income)
summary(survey$F6)
survey$income <- as.character(dplyr::recode(survey$F6,
                                      `1` = "$7,500 or less",
                                      `2` = "$7,501 to $15,000",
                                      `3` = "$15,001 to $25,000",
                                      `4` = "$25,001 to $35,000",
                                      `5` = "$35,001 to $50,000",
                                      `6` = "$50,001 or over",
                                      `7` = "Not sure/no answer/refused"))
summary(survey$income)

# age
survey$age <- as.character(dplyr::recode(survey$Q6E,
                                   `1` = "18-20",
                                   `2` = "21-24",
                                   `3` = "25-29",
                                   `4` = "30-34",
                                   `5` = "35-39",
                                   `6` = "40-49",
                                   `7` = "50-64",
                                   `8` = "65 and over",
                                   `9` = "Refused"))
table(survey$age)
### only asked of people who voted, but only 5 NAs

# race
survey$race <- as.character(dplyr::recode(survey$F7,
                                    `1` = "White, but not Hispanic",
                                    `2` = "Black, but not Hispanic",
                                    `3` = "Spanish-American (Mexican, Cuban, Puerto Rican, Central or South American",
                                    `4` = "Asian (Oriental) or Pacific Islander",
                                    `5` = "American Indian or Alaskan native",
                                    `6` = "Not sure"))
table(survey$race)

# politics (party)
survey$party <- as.character(dplyr::recode(survey$Q6C,
                                     `1` = "Republican",
                                     `2` = "Democrat",
                                     `3` = "Independent",
                                     `4` = "Other (vol.)",
                                     `5` = "Not sure"))
summary(survey$party)

# politics (ideology)
survey$ideology <- as.character(dplyr::recode(survey$F5,
                                        `1` = "Conservative",
                                        `2` = "Middle-of-the-road",
                                        `3` = "Liberal",
                                        `4` = "Not sure"))
summary(survey$ideology)

# gender
summary(survey$S1)
table(survey$S1)
survey$gender <- as.character(dplyr::recode(survey$S1,
                                      `1` = "Male",
                                      `2` = "Female"))
summary(survey$gender)

# religion
survey$religion <- as.character(dplyr::recode(survey$F4,
                                 `1` = "Protestant",
                                 `2` = "Catholic",
                                 `3` = "Jewish",
                                 `4` = "Other (SPECIFY)",
                                 `5` = "None",
                                 `6` = "Not sure/no answer/refused"))
table(survey$religion)

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- dplyr::recode(survey$Q4_1, 
                                 `1` = "Feel",
                                 `2` = "Don't feel",
                                 `3` = "Not sure")
survey$dontcount <- dplyr::recode(survey$Q4_3, 
                                  `1` = "Feel",
                                  `2` = "Don't feel",
                                  `3` = "Not sure")
survey$leftout <- dplyr::recode(survey$Q4_4, 
                                `1` = "Feel",
                                `2` = "Don't feel",
                                `3` = "Not sure")

## question place
survey$question_place <- "before party"

# subset
survey_802114 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                           "inequality", "inequality.variable", "union.self", "union.other",
                           "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                           "age", "race", "party", "ideology", "gender", "religion",
                           "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                           "question_place")]

# save file
#saveRDS(survey_802114, file = "Harris_Data/survey_802114.rds")
